const app = getApp()
let qCloud = require("../../../index.js");
Page({

    /**
     * 页面的初始数据
     */
    data: {
        imgPath: qCloud.config.STATIC_IMAGE_PATH,
        deptId: null,
        parentId: null,
        parentText: "",
        deptName: "",
        leader: "",
        phone: "",
        email: "",

        showModalStatus: false,
        deptList: [],
        searchDeptName: "",
    },

    deptNameInput(e) {
        let self = this
        self.setData({
            deptName: e.detail.value
        })
    },

    leaderInput(e) {
        let self = this
        self.setData({
            leader: e.detail.value
        })
    },

    phoneInput(e) {
        let self = this
        self.setData({
            phone: e.detail.value
        })
    },

    emailInput(e) {
        let self = this
        self.setData({
            email: e.detail.value
        })
    },

    chooseDeptName: function () {
        let self = this;
        self.getDeptList();
        self.setData({
            showModalStatus: true
        })
    },

    hideShowModal: function (e) {
        let animation = wx.createAnimation({
            duration: 200,
            timingFunction: "linear",
            delay: 0
        });
        this.animation = animation;
        animation.translateY(300).step();
        this.setData({
            animationData: animation.export(),
        });
        setTimeout(function () {
            animation.translateY(0).step();
            this.setData({
                animationData: animation.export(),
                deptList: [],
                showModalStatus: false
            })
        }.bind(this), 200);
    },

    searchDeptInput: function (e) {
        this.setData({
            searchDeptName: e.detail.value
        })
        this.getDeptList();
    },

    confirmDeptName: function (e) {
        let index = e.currentTarget.dataset.index;
        this.setData({
            parentId: this.data.deptList[index].deptId,
            parentText: this.data.deptList[index].deptName,
            showModalStatus: false
        })
    },

    /**
     * 获取部门列表
     */
    getDeptList: function () {
        let self = this;
        let params = {
            deptName: self.data.searchDeptName,
            deptId: qCloud.session.getUser().organId
        };

        self.data.deptList = [];
        app.fetchApis(self, qCloud.api.getDeptListAll, params, "GET", function (e) {
            if (e.data.code == 200) {
                self.setData({
                    deptList: e.data.data
                })
            }
        })
    },

    doSubmit(e) {
        if (this.data.deptName == "") {
            wx.showToast({
                title: "请输入部门名称",
                icon: "error"
            })
            return;
        }

        let self = this;
        let params = {
            deptId: self.data.deptId,
            parentId: self.data.parentId == null ? qCloud.session.getUser().organId : self.data.parentId,
            deptName: self.data.deptName,
            leader: self.data.leader,
            phone: self.data.phone,
            email: self.data.email,
            orderNum: 1,
            status: "0",
            deptType: "1"
        };

        app.fetchApis(self, self.data.deptId == null ? qCloud.api.addDept : qCloud.api.upateDept, params, "POST", function (e) {
            if (e.data.code == 200) {
                wx.navigateBack({
                    delta: 1
                })
            } else {
                wx.showToast({
                    title: e.data.msg,
                    icon: "error"
                })
            }
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let self = this;
        self.getDeptList();

        if (options.deptId) {
            wx.setNavigationBarTitle({
                title: "修改部门"
            })

            self.setData({
                deptId: options.deptId
            })

            app.fetchApis(self, qCloud.api.getDept + options.deptId, {}, "GET", function (e) {
                if (e.data.code == 200) {
                    self.setData({
                        deptId: e.data.data.deptId,
                        parentId: e.data.data.parentId,
                        deptName: e.data.data.deptName,
                        leader: e.data.data.leader,
                        phone: e.data.data.phone,
                        email: e.data.data.email
                    })

                    app.fetchApis(self, qCloud.api.getDept + e.data.data.parentId, {}, "GET", function (e1) {
                        if (e1.data.code == 200) {
                            self.setData({
                                parentText: e1.data.data.deptName
                            })
                        }
                    })
                }
            })
        }
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {
        wx.redirectTo({
            url: "/pages/enterprise/department/lists",
        })
    },

})
